$(function () {
  var key = getSearch().key
  render()

  function render() {
    $('.lt_product').html('<div class="loading"></div>')
    var param = {
      proName: key,
      page: 1,
      pageSize: 100,
    }
    if ($('a.active').length === 1) {
      var type = $('a.active').data('type')
      var value = $('a.active').children().hasClass('fa-angle-down') ? 2 : 1
      param[type] = value
    }
    $.ajax({
      url: '/product/queryProduct',
      type: 'get',
      data: param,
      success: function (info) {
        console.log(info)
        setTimeout(function () {
          $('.lt_product').html(template('tmp', info))
          $('[name=search]').val(key)
          console.log(key)
        },1000)
      }
    })
  }


  $('.btn_search').on('click', function () {
    key = $('[name=search]').val().trim()
    if (!key) {
      return
    }
    location.href = 'searchList.html?key=' + key
  })

  $('.sort a[data-type]').on('click', function () {
    if (!$(this).hasClass('active')) {
      $(this).addClass('active')
      $(this).siblings().removeClass('active')
      $(this).siblings().children().addClass('fa-angle-down').removeClass('fa-angle-up')
    } else {
      $(this).children().toggleClass('fa-angle-up').toggleClass('fa-angle-down')
    }
    render()
  })
})